package com.quma.boss.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.quma.boss.R;
import com.quma.boss.utils.StringUtils;


/**
 * Description MyToolBar toolBar的自定义控件
 * Created by Taro on 2017/6/26.
 */
public class MyToolBar extends RelativeLayout {
    private String mTitle;
    private boolean isLeftShow;
    private boolean isRightShow;

    private int mLeftResourse;
    private int mRightResourse;

    private TextView tvTitle;
    private ImageView btnBack;
    private ImageView btnMore;
    private TextView tvLeft;
    private TextView tvRight;

    private String mLeftText;
    private String mRightText;
    private int mBgColor;
    private View view;
    private Context context;


    public MyToolBar(Context context) {
        super(context);
    }

    public MyToolBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public MyToolBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        this.context = context;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.myToolBar, defStyle, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            if (attr == R.styleable.myToolBar_mtbTitle) {//标题
                mTitle = a.getString(attr);

            } else if (attr == R.styleable.myToolBar_mtbLeftBtn) {//左边按钮
                mLeftResourse = a.getResourceId(attr, R.mipmap.icon_action_back);

            } else if (attr == R.styleable.myToolBar_mtbRightBtn) {//右边按钮
                mRightResourse = a.getResourceId(attr, R.mipmap.icon_more);

            } else if (attr == R.styleable.myToolBar_isLeftShow) {//是否显示左边按钮
                isLeftShow = a.getBoolean(attr, false);

            } else if (attr == R.styleable.myToolBar_isRightShow) {//是否显示右边按钮
                isRightShow = a.getBoolean(attr, false);

            } else if (attr == R.styleable.myToolBar_mtbLeftText) {//左边文字
                mLeftText = a.getString(attr);

            } else if (attr == R.styleable.myToolBar_mtbRightText) {//右边文字
                mRightText = a.getString(attr);

            }
        }
        a.recycle();
        view = View.inflate(context, R.layout.base_mytoolbar, this);
        tvTitle =  findViewById(R.id.tvTitle);
        btnBack =  findViewById(R.id.btnBack);
        btnMore =  findViewById(R.id.btnMore);
        tvLeft = findViewById(R.id.tvLeft);
        tvRight =  findViewById(R.id.tvRight);
        initView();
    }

    private void initView() {
        //如果标题不为空，则设置标题
        if (!StringUtils.isEmpty(mTitle)) {
            tvTitle.setText(mTitle);
        }
        //是否显示左边按钮
        if (isLeftShow) {
            //显示
            btnBack.setVisibility(VISIBLE);
            if (mLeftResourse != 0 && mLeftResourse != R.mipmap.icon_action_back) {
                //如果自定义资源则使用自定义资源
                btnBack.setImageResource(mLeftResourse);
            }
            if (!StringUtils.isEmpty(mLeftText)) {
                //如果自定义文字则使用文字，屏蔽图像
                btnBack.setVisibility(GONE);
                tvLeft.setVisibility(VISIBLE);
                tvLeft.setText(mLeftText);
            }
        } else {
            //不显示
            btnBack.setVisibility(GONE);
            tvLeft.setVisibility(GONE);
        }

        //是否显示右边边按钮
        if (isRightShow) {
            //显示
            btnMore.setVisibility(VISIBLE);
            if (mRightResourse != 0 && mRightResourse != R.mipmap.icon_more) {
                //如果自定义资源则使用自定义资源
                btnMore.setImageResource(mRightResourse);
            }
            if (!StringUtils.isEmpty(mRightText)) {
                //如果自定义文字则使用文字，屏蔽图像
                btnMore.setVisibility(GONE);
                tvRight.setVisibility(VISIBLE);
                tvRight.setText(mRightText);
            }
        } else {
            //不显示
            btnMore.setVisibility(GONE);
            tvRight.setVisibility(GONE);
        }

        if (mBgColor != 0) {
            view.setBackgroundColor(mBgColor);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                view.setBackgroundColor(context.getColor(R.color.white));
            }
        }

    }

    public String getmTitle() {
        return mTitle;
    }

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
        this.tvTitle.setVisibility(VISIBLE);
        initView();
        invalidate();
    }


    public int getmBgColor() {
        return mBgColor;
    }

    public void setmBgColor(int mBgColor) {
        this.mBgColor = mBgColor;
        initView();
        invalidate();
    }

    public boolean isLeftShow() {
        return isLeftShow;
    }

    public void setLeftShow(boolean isLeftShow) {
        this.isLeftShow = isLeftShow;
        initView();
        invalidate();
    }

    public boolean isRightShow() {
        return isRightShow;
    }

    public void setRightShow(boolean isRightShow) {
        this.isRightShow = isRightShow;
        initView();
        invalidate();
    }

    public int getmLeftBtn() {
        return mLeftResourse;
    }

    public void setmLeftBtn(int mLeftBtn) {
        this.mLeftResourse = mLeftBtn;
        initView();
        invalidate();
    }

    public int getmRightBtn() {
        return mRightResourse;
    }

    public void setmRightBtn(int mRightBtn) {
        this.mRightResourse = mRightBtn;
        initView();
        invalidate();
    }

    public String getmLeftText() {
        return mLeftText;
    }

    public void setmLeftText(String mLeftText) {
        this.mLeftText = mLeftText;
        initView();
        invalidate();
    }

    public String getmRightText() {
        return mRightText;
    }

    public void setmRightText(String mRightText) {
        this.mRightText = mRightText;
        initView();
        invalidate();
    }

    public ImageView getBtnBack() {
        return btnBack;
    }

    public ImageView getBtnMore() {
        return btnMore;
    }

    public TextView getTvTitle() {
        return tvTitle;
    }

    public TextView getTvLeft() {
        return tvLeft;
    }

    public TextView getTvRight() {
        return tvRight;
    }


}